﻿
@model List<Subplicity.ClassService.Class>

@{
    
    var newlist = Model.GroupBy(x => new { x.Location.Name, x.StartDateTime.Value.Date })
                           .Select(y => new
                           {
                               LocationName = y.Key.Name,
                               ClassDay = y.Key.Date,
                               ListClass = y.ToList()

                           });
                       
}

@{
    ViewBag.Title = "Schedule";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<table border="1">
    <thead>
        <tr>
            <th>Class Name</th>
            <th>Start Time</th>
            <th>End Time</th>
            <th>ClassScheduleID</th>
            <th>ID Used to get a sub for this class</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in newlist)
        {
            <tr>
                <th colspan="3" style="background-color: lightblue">@item.LocationName  -  @item.ClassDay.DayOfWeek - @item.ClassDay.ToShortDateString()</th>
            </tr>
            foreach (var cl in item.ListClass)
            {
            <tr>
                <td>@cl.ClassDescription.Name</td>
                <td>@cl.StartDateTime.Value.ToShortTimeString()</td>
                <td>@cl.EndDateTime.Value.ToShortTimeString()</td>
                <td>@cl.ClassScheduleID.ToString()</td>
                <td><input type="button" value="@cl.ID.Value.ToString()" onclick="javascript: AskForSub(@cl.ID.Value.ToString());" /></td>
            </tr>
            }
        }

    </tbody>
    <tfoot></tfoot>
</table>


<script type="text/javascript">
    function AskForSub(clid) {
        var id=clid.val;
        $.ajax({
            url: '/Schedule/SubRequest?classID=' + id,
            type: 'POST',
            contentType: 'application/json',            
            success: function (result) {
                var obj = jQuery.parseJSON(result);
                if (obj.success) {
                    document.location = "/Schedule";
                } else {
                   alert("Wrong");
                }
            }
        });
        return false;

    }
    
    
</script>